package RPPM.dao.impl;

import RPPM.dao.TeaDao;
import RPPM.domain.Course;
import RPPM.domain.Teacher;
import RPPM.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class TeaDaoImpl implements TeaDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public Teacher findTeacherById(Teacher tobechecked) {

        String sql = "select * from teacher where Tea_id = ?";
        Teacher teacher;
        try {

            teacher = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Teacher>(Teacher.class), tobechecked.getTea_id());

        } catch (Exception e){
            teacher = null;
            return teacher;
        }
        return teacher;
    }

    @Override
    public List<Map<String, Object>> findCourseByTeaid(Teacher teacher) {
        String sql = "select Cou_id, Cou_name, Cou_time, Cou_location from course where Tea_id = ?";
        try {
            List<Map<String, Object>> courses = jdbcTemplate.queryForList(sql, teacher.getTea_id());
            return courses;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> findCoursesByname(int tea_id, String searchText) {
        String sql = "select Cou_id, Cou_name, Cou_time, Cou_location from course where Tea_id = ? and Cou_name like ?";
        try {
            List<Map<String, Object>> courses = jdbcTemplate.queryForList(sql, tea_id, "%"+searchText+"%");
            return courses;
        } catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean teaChangePwdById(Teacher tobechecked, String newPwd) {
        System.out.println("teaChangeById");
        System.out.println(tobechecked.getTea_id()+"  "+tobechecked.getTea_password()+"  "+newPwd);
        String sql = "select * from teacher where Tea_id = ? and Tea_password = ?";
        String sql1 = "update teacher set Tea_password = ? where Tea_id = ?";
        try {
            Teacher teacher = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Teacher>(Teacher.class),tobechecked.getTea_id(), tobechecked.getTea_password());
            int count = jdbcTemplate.update(sql1, newPwd, tobechecked.getTea_id());
            return (count == 1)?true:false;
        } catch (Exception e){
            return false;
        }
    }
    /*
    添加培训计划
     */
    @Override
    public boolean CourseInhance(int Cou_id, String Cou_name, String Cou_time, String Cou_location, Teacher teacher) {
        System.out.println("CourseInhance");

        String sql = "insert into Course (Cou_id ,Cou_name, Cou_time  ,Cou_location ,Cou_teacher) values ('"+Cou_id+"','"+Cou_name+"','"+Cou_time+"','"+Cou_location+"','"+teacher.getTea_name()+"')";

        int count = jdbcTemplate.update(sql);

        return (count > 0)?true:false;
    }
    /*
       修改培训计划
        */
    @Override
    public boolean CourseUpdate(String newtime,String newlocal,Course course,Teacher teacher) {
        System.out.println("ScoreToDataBase");
        String sql = "update Course set Cou_time = '"+newtime+"' , Cou_location = '"+newlocal+"' where Cou_id = (select Cou_id from Course where Cou_name = '"+course.getCou_name()+"') and Tea_id = '"+teacher.getTea_id()+"' ";

        int count = jdbcTemplate.update(sql);

        return (count > 0)?true:false;
    }


}
